#include <gtkmm.h>
#include <iostream>
#include <fstream>
#include "SmartArray.h"
#include "SmartString.h"
#include "Palette.h"
#include "palrender.h"
#include "AdvanceMap.h"

class Phoenix:public Gtk::Window
{
	SmartArray<Palette> mPalettes;
	Gtk::VBox items;
	Gtk::ComboBoxText widget_pals;
	Gtk::MenuBar mMenu;
	Gtk::Menu mFileContainer;
	Gtk::MenuItem mFile, mNew, mSave, mOpen, mQuit;
	Gtk::Menu mPaletteContainer;
	Gtk::MenuItem mPaletteM, mDestroy, mEdit;
	Palette_Renderer mRender;
	Palette& getActivePalette()
	{
		SmartString name(widget_pals.get_active_text().c_str());
		for( int i=0, end = mPalettes.Length(); i<end; i++)
			if( mPalettes[i].Name() == name)
				return mPalettes[i];
		throw Reference<Palette>::NullReference();
	}
public:
	static SmartArray<PaletteFormat> gAllocaters;
	virtual void delItem()
	{
		mPalettes.Remove( static_cast<Reference<Palette> >( mRender));//mRender still has a reference...
		widget_pals.remove_text( widget_pals.get_active_text());
		if( mPalettes.Length() > 0)
			widget_pals.set_active_text( static_cast<const char*>(mPalettes[-1].Name()));
		else newItem();
	}
	virtual void newItem()
	{
		if( mPalettes.Length() < 16 )
		{
			const char* numbers("0123456789ABCDEF");
			Glib::ustring text = static_cast<const char*>( SmartString("Palette: ").Append( numbers[ mPalettes.Length()]));
			mPalettes.Append(new Palette(text.c_str()));
			widget_pals.append_text( text);
			widget_pals.set_active_text( text);
		}
	}
	virtual void on_change()
	{
		if( widget_pals.get_active_text().length() > 0)
			mRender = &getActivePalette();
	}
	virtual void save()
	{
		gAllocaters[-1].Save( getActivePalette(), SmartArray<SmartString>().Append( new SmartString("Sample.pal")));
	}
	virtual void open()
	{
		gAllocaters[-1].Open( getActivePalette(), SmartArray<SmartString>().Append( new SmartString("Sample.pal")));
	}
	virtual void editItem()
	{
		log_mem_func("editItem");
	}
	Phoenix(char** args):
	items(false,5),mFile("File"),mNew("New Palette"),
	mSave("Save"),mOpen("Open"),mQuit("Quit"),
	mPaletteM("Palette"),mDestroy("Destroy"),mEdit("Edit")
	{
		{
			set_title("Phoenix v3 Alpha");
			{
				mMenu.add(mFile);
				{
					mFileContainer.append(mNew);
					mNew.signal_activate().connect( sigc::mem_fun( *this, &Phoenix::newItem ));
					mFileContainer.append(mSave);
					mSave.signal_activate().connect( sigc::mem_fun( *this, &Phoenix::save));
					mFileContainer.append(mOpen);
					mOpen.signal_activate().connect( sigc::mem_fun( *this, &Phoenix::open));
					mFileContainer.append(mQuit);
					mQuit.signal_activate().connect( sigc::ptr_fun( &Gtk::Main::quit));
					mFile.set_submenu(mFileContainer);
				}
				mMenu.add(mPaletteM);
				{
					mPaletteContainer.append(mDestroy);
					mDestroy.signal_activate().connect( sigc::mem_fun( *this, &Phoenix::delItem));
					mPaletteContainer.append(mEdit);
					mEdit.signal_activate().connect( sigc::mem_fun( *this, &Phoenix::editItem));
					mPaletteM.set_submenu(mPaletteContainer);
				}
			}
			widget_pals.signal_changed().connect( sigc::mem_fun( *this, &Phoenix::on_change));
			items.pack_start(mMenu,false,true,0);
			items.pack_start(mRender,true,true,5);
			items.pack_start(widget_pals,false,true,1);
			items.set_border_width(1);
			add(items);
		}
		show_all();
		newItem();
	}
};

SmartArray<PaletteFormat> Phoenix::gAllocaters;

int main(int argc,char** argv)
{
	if(PHOENIX_DEBUG)
	{
		std::cout << "Welcome to Phoenix, build "
		          << PHOENIX_VERSION
		          << "--debug." << std::endl;
	}
	Phoenix::gAllocaters.Append(new AdvanceMap);
	Gtk::Main kit(argc,argv);
	Phoenix app(argv);
	Gtk::Main::run(app);
	return 0;
}